# -*- coding: UTF-8 -*-
"""
@auth:buxiangjie
@date:2020-10-18 13:37:00
@describe: 
"""
import os
import sys

import uvicorn

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from fastapi import FastAPI
from app.db.conn import DataBasePool
from route import saas, statistics
from app.tasks.task import scheduler
from app.log.ulog import logger_

app = FastAPI(title="测试工具", description="测试")

app.include_router(saas.router, prefix="/saas", tags=["saas"])
app.include_router(statistics.router, prefix="/statistics", tags=["statistics"])


@app.on_event("startup")
async def init_pool():
	"""
	初始化数据库连接池
	"""
	await DataBasePool().init()


@app.on_event("startup")
async def start_event():
	"""
	启动定时任务
	"""
	scheduler.start()
	logger_.info("定时任务启动成功")


@app.on_event("shutdown")
async def tear_down():
	"""
	关闭数据库连接池
	"""
	await DataBasePool().tear_down()


if __name__ == '__main__':
	uvicorn.run(
		"main:app",
		log_level="debug",
		reload=True,
		host="0.0.0.0",
		port=8817
	)
